import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../system/fontsize_provider.dart';

class FontSizeSettingScreen extends StatefulWidget {
  const FontSizeSettingScreen({super.key});

  @override
  State<FontSizeSettingScreen> createState() => _FontSizeSettingScreenState();
}

class _FontSizeSettingScreenState extends State<FontSizeSettingScreen> {
  late int _currentValue;
  late double _fontScale;

  @override
  void initState() {
    super.initState();
    // 从全局状态获取当前值并正确转换为0-10范围
    final fontScale = Provider.of<FontSizeProvider>(context, listen: false).fontScale;
    print('fontScale----$fontScale');
    // 这里的转换公式要和FontSizeProvider中的映射完全相反
    // 因为FontSizeProvider中是: _fontScale = 0.5 + (value / 10) * 1.0
    // 所以反向计算就是: value = (fontScale - 0.5) * 10
    _currentValue = ((fontScale - 0.5) * 10).round();
  }

  // 转换当前值到0-10范围
  void _updateValue(double value) {
    setState(() {
      _currentValue = value.toInt().clamp(0, 10); // 确保值在0-10之间
    });
    // 更新全局状态
    Provider.of<FontSizeProvider>(context, listen: false)
        .setFontScale(_currentValue);
  }

  @override
  Widget build(BuildContext context) {
    _fontScale = Provider.of<FontSizeProvider>(context).fontScale;
    return Scaffold(
      appBar: AppBar(
        title: Text('Font Size Settings',style: TextStyle(fontSize: 18 * _fontScale),),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back, color: Colors.white),
          onPressed: () => Navigator.pop(context),
        ),
        actions: [
          IconButton(
            icon: const Icon(Icons.home, color: Colors.white),
            onPressed: () => Navigator.pushNamed(context, '/home'),
          ),
        ],
        backgroundColor: const Color(0xFF008363),
      ),
      body: Padding(
        padding: const EdgeInsets.all(32),
        child: Column(
          children: [
            // 显示当前值
            Text(
              'Current Size: ${_currentValue.round()}',
              style: TextStyle(fontSize: 13 * _fontScale, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 40),
            
            // 加减按钮控制
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(
                  onPressed: _currentValue > 0 ? () => _updateValue(_currentValue - 1) : null,
                  style: ElevatedButton.styleFrom(
                    // backgroundColor: const Color(0xFF008363),
                    padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
                  ),
                  child: const Text('-', style: TextStyle(fontSize: 24)),
                ),
                const SizedBox(width: 20),
                // 滑动条控制  
                Expanded(
                  child: Slider(
                    value: _currentValue.truncateToDouble(),
                    min: 0,
                    max: 10,
                    divisions: 10,
                    label: _currentValue.round().toString(),
                    onChanged: _updateValue,
                  ),
                ),
                const SizedBox(width: 20),
                ElevatedButton(
                  onPressed: _currentValue < 10 ? () => _updateValue(_currentValue + 1) : null,
                  style: ElevatedButton.styleFrom(
                    // backgroundColor: const Color(0xFF008363),
                    padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
                  ),
                  child: const Text('+', style: TextStyle(fontSize: 24)),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
